<template>
	<view class="home">
		<!-- 状态栏 -->
		<page-status></page-status>
		<!-- 导航条 -->
		<page-header url="/pages/tabBar/home/home" @camera="onClickCameraIcon" @scanCode="onClickScanCodeIcon"></page-header>
		
		<!-- 轮播图 -->
		<view class="swiper">
			<view class="swiper-box">
				<!-- 图片区域 -->
				<swiper circular autoplay @change="swiperChange">
					<swiper-item v-for="(swiper, index) of swiperList" :key="index">
						<image :src="swiper.img"></image>
					</swiper-item>
				</swiper>
				<!-- 图片底部的进度条 -->
				<view class="progress-bar">
					<view class="progress" :style="{ 'width': currentSwiper / swiperList.length * 100 + '%' }"/>
				</view>
			</view>
		</view>
		
		<!-- 分类列表 -->
		<view class="swiper-category">
			<view class="swiper-box">
				<swiper circular>
					<swiper-item v-for="(categoryGroup, index) of categoryGroupArr" :key="index">
						<view class="category-list">
							<view class="category" v-for="(item, z) of categoryGroup" :key="item + z" @tap="toCategory(item)">
								<view class="img"><image :src="item.img"></image></view>
								<text class="text">{{ item.name }}</text>
							</view>
						</view>
					</swiper-item>
				</swiper>
			</view>
		</view>
		
		<!-- 广告栏 -->
		<view v-if="promotion.length > 0" class="banner">
			<image src="/static/img/category/ad.jpg"></image>
		</view>
		
		<!-- 活动区 -->
		<view class="promotion" v-if="promotion.length > 0">
			<view class="text">优惠活动</view>
			<view class="list">
				<view
					class="column"
					v-for="(row, index) in promotion"
					@tap="toPromotion(row)"
					:key="index"
				>
					<view class="top">
						<view class="title">{{ row.title }}</view>
					</view>
					<view class="left">
						<view class="ad">{{ row.ad }}</view>
						<view class="into">点击进入</view>
					</view>
					<view class="right"><image :src="row.img"></image></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import HomeHeader from "@/components/searchGroup.vue"
	import homeRequestModule from "@/utils/module/home.js"
	
	export default {
		name: "home",
		components: { "page-header": HomeHeader },
		data() {
			return {
				swiperList: [{}], // 轮播
				currentSwiper: 1, // 当前是第几个轮播图
				categoryList: [], // 分类
				promotion: [] // 活动
			}
		},
		computed: {
			categoryGroupArr() {
				const arr = [], totalGroupNum = (Math.floor(this.categoryList.length / 8))
				for(let i=0; i <= totalGroupNum; i++) {
					arr.push([])
				}
				this.categoryList.forEach((item, index) => {
					const currentGroupIndex = Math.floor(index / 8)
					arr[currentGroupIndex].push(item)
				})
				return arr
			}
		},
		onLoad: function() {
			this.initPage();
		},
		methods: {
			// 获取首页数据（轮播、分类与活动）
			async initPage() {
				const res = await homeRequestModule.getMallData()
				this.swiperList = res.data.swiperList;
				this.categoryList = res.data.categoryList.concat(res.data.categoryList.slice(0, 4));
				this.promotion = res.data.promotion;
			},
			onClickCameraIcon(data) {
				console.log("相机成功", data);
				uni.showToast({ title: "相机成功", icon: "none" })
			},
			onClickScanCodeIcon(data) {
				console.log("扫码成功", data.data);
				uni.showToast({ title: "扫码成功", icon: "none" })
			},
			// 轮播图的change事件，让下面的白条动态变化
			swiperChange(event) {
				this.currentSwiper = event.detail.current + 1;
			},
			//分类跳转
			toCategory(e) {
				uni.navigateTo({
					url: "../../goods/goodsList?name=" + e.name
				})
			},
			//推荐商品跳转
			toPromotion(e) {
				uni.showToast({ title: e.title, icon: 'none' });
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* 轮播图 */
	.swiper {
		display: flex;
		justify-content: center;
		width: 100%;
		/* 包裹轮播组件的盒子，用于居中轮播组件 */
		.swiper-box {
			position: relative;
			width: 92%;
			height: 30.7vw;
			overflow: hidden;
			border-radius: 10upx;
			box-shadow: 0upx 8upx 25upx rgba(0, 0, 0, 0.2);
			swiper {
				width: 100%;
				height: 100%;
				image {
					width: 100%;
					height: 100%;
				}
			}
			/* 轮播进度条 */
			.progress-bar {
				position: absolute;
				bottom: 20upx;
				left: 20upx;
				background-color: rgba(255, 255, 255, 0.4);
				width: 150upx;
				height: 5upx;
				border-radius: 3upx;
				overflow: hidden;
				display: flex;
				.progress {
					width: 0upx;
					background-color: rgba(255, 255, 255, 1);
					transition: all 0.3s ease-out;
				}
			}
		}
	}
	
	/* 分类列表中的轮播 */
	.swiper-category {
		display: flex;
		justify-content: center;
		width: 100%;
		margin-top: 40rpx;
		.swiper-box {
			position: relative;
			width: 92%;
			height: 420rpx;
			overflow: hidden;
			border-radius: 10upx;
			box-shadow: 0upx 8upx 25upx rgba(0, 0, 0, 0.2);
			swiper {
				width: 100%;
				height: 100%;
				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}
	/* 分类列表 */
	.category-list {
		width: 92%;
		margin: 0 4%;
		padding: 0 0 30upx 0;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		.category {
			width: 25%;
			margin-top: 50upx;
			display: flex;
			flex-wrap: wrap;
			.img {
				width: 100%;
				display: flex;
				justify-content: center;
				image {
					width: 11vw;
					height: 11vw;
				}
			}
			.text {
				margin-top: 16upx;
				width: 100%;
				display: flex;
				justify-content: center;
				font-size: 24upx;
				color: $public-title-black;
			}
		}
	}
	/* 广告栏 */
	.banner {
		width: 92%;
		margin: 40upx 4%;
		image {
			width: 100%;
			height: 36vw;
			border-radius: 4upx;
			box-shadow: 0upx 5upx 25upx rgba(0, 0, 0, 0.3);
		}
	}
	/* 热门商品 */
	.promotion {
		width: 92%;
		margin: 10upx 4% 30upx 4%;
		.text {
			width: 100%;
			height: 60upx;
			font-size: 34upx;
			font-weight: 600;
			margin: 10upx;
		}
		.list {
			width: 100%;
			display: flex;
			justify-content: space-between;
			.column {
				width: 43%;
				padding: 15upx 3%;
				background-color: #ebf9f9;
				border-radius: 10upx;
				overflow: hidden;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				.top {
					width: 100%;
					height: 40upx;
					display: flex;
					align-items: center;
					margin-bottom: 5upx;
					.title {
						font-size: 30upx;
					}	
				}
				.left {
					width: 50%;
					height: 18.86vw;
					display: flex;
					flex-wrap: wrap;
					align-content: space-between;
					.ad {
						margin-top: 5upx;
						width: 100%;
						font-size: 22upx;
						color: #acb0b0;
					}
					.into {
						width: 100%;
						font-size: 24upx;
						color: #aaa;
						margin-bottom: 5upx;
					}
				}
				.right {
					width: 18.86vw;
					height: 18.86vw;
					image {
						width: 18.86vw;
						height: 18.86vw;
					}
				}
			}
		}
	}
</style>
